package larrys.pomodoro.controller.pages;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Validator;

import larrys.pomodoro.Constant;
import larrys.pomodoro.domain.entity.Activity;
import larrys.pomodoro.domain.entity.PomodoroUser;
import larrys.pomodoro.domain.entity.Round;
import larrys.pomodoro.domain.service.ActivityService;
import larrys.pomodoro.domain.service.RoundService;
import larrys.pomodoro.util.SecurityUtils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class RoundController {

	private static Log logger = LogFactory.getLog(RoundController.class);

	@Autowired
	private MessageSource messages;

	@Autowired
	private Validator validator;

	@Autowired
	private RoundService roundService;

	@Autowired
	private ActivityService activityService;

	private void setRoundModelActive(Model model) {
		model.addAttribute(Constant.ACTIVIE_ATTRNAME, "My Round");
	}

	private void setRelatedActivity(Model model, Round round) {
		if (round != null) {
			Activity relatedActivity = activityService.getActivityById(round
					.getActivityId());
			model.addAttribute("activity", relatedActivity);
		}
	}

	@RequestMapping(value = "/round", method = RequestMethod.GET)
	public String round(Model model) {
		setRoundModelActive(model);
		PomodoroUser user = SecurityUtils.getCurrentUser();
		Round runningRound = roundService.getRunningRound(user.getId());
		setRelatedActivity(model, runningRound);
		model.addAttribute("round", runningRound);
		return "round/round";
	}

	@RequestMapping(value = "/gotoround", method = RequestMethod.GET)
	public String gotoRound(Model model,
			@RequestParam("activity_id") long activityId) {
		setRoundModelActive(model);
		PomodoroUser user = SecurityUtils.getCurrentUser();
		logger.debug("Trying to goto round for:" + user + ", with Activity[id:"
				+ activityId + "]");

		Round roundAboutToRun;
		Round runningRound = roundService.getRunningRound(user.getId());
		if (runningRound != null) {
			roundAboutToRun = runningRound;
		} else {
			roundAboutToRun = roundService.openRound(user.getId(), activityId);
		}
		model.addAttribute("round", roundAboutToRun);
		setRelatedActivity(model, roundAboutToRun);
		return "round/round";
	}

	@RequestMapping(value = "/finishround", method = RequestMethod.GET)
	public String finishRound(
			Model model,
			@RequestParam(value = "full_round", required = false) boolean fullRound) {
		setRoundModelActive(model);
		PomodoroUser user = SecurityUtils.getCurrentUser();
		Round runningRound = roundService.getRunningRound(user.getId());
		if (runningRound != null) {
			logger.debug("Trying to finish " + runningRound + ", for " + user);
			roundService.finishRound(runningRound.getId(), fullRound);
			logger.debug(runningRound + " finished, for " + user);
			model.addAttribute("roundId", runningRound.getId());
			return "round/roundcomment";
		} else {
			return "redirect:/pages/round";
		}
	}

	@RequestMapping(value = "/interruptround", method = RequestMethod.GET)
	public String interruptRound(Model model) {
		setRoundModelActive(model);
		PomodoroUser user = SecurityUtils.getCurrentUser();
		Round runningRound = roundService.getRunningRound(user.getId());
		if (runningRound != null) {
			logger.debug("Trying to interrupt " + runningRound + ", for "
					+ user);
			roundService.interruptRound(runningRound.getId());
			logger.debug(runningRound + " interrupted, for " + user);
			model.addAttribute("roundId", runningRound.getId());
			return "round/roundcomment";
		} else {
			return "redirect:/pages/round";
		}
	}

	@RequestMapping(value = "/roundcomment", method = RequestMethod.POST)
	public String roundcomment(Model model, HttpServletRequest req,
			HttpServletResponse rep, @RequestParam("round_id") long roundId,
			@RequestParam(value = "comment", required = false) String comment)
			throws IOException {
		logger.debug("Trying to add comment to Round[id:" + roundId + "]");
		if (comment != null && !comment.isEmpty()) {
			roundService.addRoundComment(roundId, comment);
		}
		logger.debug("Finished add comment to Round[id:" + roundId + "]");
		return "redirect:/pages/activity";
	}
}
